package com.tl.sun.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.tl.sun.AppContext;

/**
 * Created by apple on 16/10/15.
 */

public class MDAccountMgr {

    private static MDAccountMgr instance;


    private String loginName;

    private String loginPwd;


    private String radiusPass;

    private boolean isPassowrd;

    private String sessionId;

    private boolean login;
    private String deviceToken;//友盟 token

    private String loginType;

    private String openid;

    private String username;
    private String unionid;
    private String isSenior;


    public String getDeviceToken() {
        return deviceToken;
    }

    public void setDeviceToken(String deviceToken) {
        this.deviceToken = deviceToken;
    }

    public static MDAccountMgr getInstance() {
        if (instance == null) {
            synchronized (MDAccountMgr.class) {
                instance = new MDAccountMgr();
            }
        }
        return instance;
    }

    private MDAccountMgr() {
        getCache(AppContext.getInstance());
    }

    public void clearCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.apply();
    }

    public void clearAccountInfo() {
        loginName = "";
        loginPwd = "";
        radiusPass = "";
        loginType = "";
        isPassowrd = false;
        sessionId = "";
        openid = "";
        username = "";
        unionid = "";
        isSenior = "";
        login = false;
        clearCache(AppContext.getInstance());
    }


    public void writeToCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(MDConstant.LOGIN_NAME, loginName);
        editor.putString(MDConstant.LOGIN_PASSWORD, loginPwd);
        editor.putString(MDConstant.RADIUS_PASSWORD, radiusPass);
        editor.putString(MDConstant.SESSION_ID, sessionId);
        editor.putBoolean(MDConstant.IS_PASSWORD, isPassowrd);
        editor.putBoolean(MDConstant.IS_LOGIN, login);
        editor.putString(MDConstant.DEVICE_TOKEN, deviceToken);
        editor.putString(MDConstant.LOGIN_TYPE, loginType);
        editor.putString(MDConstant.WX_USERNAME, username);
        editor.putString(MDConstant.OPEN_ID, openid);
        editor.putString(MDConstant.UNIONID, unionid);
        editor.putString(MDConstant.SENIOR, isSenior);

        editor.apply();
    }

    public void getCache(Context context) {
        SharedPreferences sharedata = context.getSharedPreferences(MDConstant.LOGIN_INFO, 0);
        loginName = sharedata.getString(MDConstant.LOGIN_NAME, null);
        loginPwd = sharedata.getString(MDConstant.LOGIN_PASSWORD, null);
        radiusPass = sharedata.getString(MDConstant.RADIUS_PASSWORD, "");
        sessionId = sharedata.getString(MDConstant.SESSION_ID, null);
        login = sharedata.getBoolean(MDConstant.IS_LOGIN, false);
        isPassowrd = sharedata.getBoolean(MDConstant.IS_PASSWORD, false);
        deviceToken = sharedata.getString(MDConstant.DEVICE_TOKEN, "");
        loginType = sharedata.getString(MDConstant.LOGIN_TYPE, "0");
        openid = sharedata.getString(MDConstant.OPEN_ID, "");
        username = sharedata.getString(MDConstant.WX_USERNAME, "");
        unionid = sharedata.getString(MDConstant.UNIONID, "");
        isSenior = sharedata.getString(MDConstant.SENIOR, "");
    }


    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getLoginPwd() {
        return loginPwd;
    }

    public void setLoginPwd(String loginPwd) {
        this.loginPwd = loginPwd;
    }

    public boolean isPassowrd() {
        return isPassowrd;
    }

    public void setPassowrd(boolean passowrd) {
        isPassowrd = passowrd;
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean login) {
        this.login = login;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getLoginType() {
        return loginType;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public String getRadiusPass() {
        return radiusPass;
    }

    public void setRadiusPass(String radiusPass) {
        this.radiusPass = radiusPass;
    }

    public String getIsSenior() {
        return isSenior == null ? "" : isSenior;
    }

    public void setIsSenior(String isSenior) {
        this.isSenior = isSenior;
    }
}
